<template>
  <el-row>
    <el-col :span="11">
      <el-breadcrumb separator="/" style="margin: 0 0 20px 0">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>经纪人</el-breadcrumb-item>
      </el-breadcrumb>
      <el-input placeholder="请输入ID" size="small" style="width: 40%" v-model.trim="sousuo"></el-input>
      <el-button type="primary" size="small" @click="sousuoid()">搜索</el-button>
    </el-col>
    <el-col :span="11">
      <h1>经纪人</h1>
    </el-col>
    <el-col :span="2">
      <el-button type="primary" size="small" @click="dialogFormAdd = true">添加经纪人</el-button>
    </el-col>
    <el-col :span="24" style="margin:  10px 0 3% 0">
      <el-table
        :data="jjrData"
        style="width: 100%;">
        <el-table-column
          type="selection"
          width="40">
        </el-table-column>
        <el-table-column
          prop="number"
          label="经纪人ID">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="80">
        </el-table-column>
        <el-table-column
          prop="level"
          label="星级"
          width="80">
        </el-table-column>
        <el-table-column
          prop="phone"
          label="联系电话">
        </el-table-column>
        <el-table-column
          prop="company"
          label="所属公司">
        </el-table-column>
        <el-table-column
          prop="store"
          label="所属门店">
        </el-table-column>
        <el-table-column
          prop="serviceRange"
          label="服务商圈">
        </el-table-column>
        <el-table-column
          prop="estate"
          label="主营楼盘">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="lookatAgent(scope.$index, scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="upatAgent(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delatAgent(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>

    <el-col :span="24" style="text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5,10]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <el-dialog title="添加经纪人" :visible.sync="dialogFormAdd">
        <el-form :model="formAdd" style="width: 80%" :rules="rules1" ref="formAdd">
          <el-form-item label="经纪人名称 :" label-width="20%" prop="name">
            <el-input v-model="formAdd.name"></el-input>
          </el-form-item>
          <el-form-item label="星级 :" label-width="20%" prop="level">
            <el-input v-model="formAdd.level" placeholder="星级最高为5"></el-input>
          </el-form-item>
          <el-form-item label="联系电话 :" label-width="20%" prop="phone">
            <el-input v-model="formAdd.phone"></el-input>
          </el-form-item>
          <el-form-item label="所属公司 :" label-width="20%" prop="company">
            <el-input v-model="formAdd.company"></el-input>
          </el-form-item>
          <el-form-item label="所属门店 :" label-width="20%" prop="store">
            <el-input v-model="formAdd.store"></el-input>
          </el-form-item>
          <el-form-item label="服务商圈 :" label-width="20%" prop="serviceRange">
            <el-input v-model="formAdd.serviceRange"></el-input>
          </el-form-item>
          <el-form-item label="主营楼盘 :" label-width="20%" prop="estate">
            <el-input v-model="formAdd.estate"></el-input>
          </el-form-item>
          <el-form-item label="从业时间 :" label-width="20%" prop="seniority">
            <el-input v-model="formAdd.seniority"></el-input>
          </el-form-item>
          <el-form-item label="服务宣言 :" label-width="20%" prop="declaration">
            <el-input v-model="formAdd.declaration"></el-input>
          </el-form-item>

          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="formAdd.headImg" :src="formAdd.headImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('formAdd')">取 消</el-button>
          <el-button type="primary" @click="addatAgent('formAdd')">添 加</el-button>
        </div>
      </el-dialog>
      <el-dialog title="修改经纪人" :visible.sync="dialogFormUp">
        <el-form :model="formUp" style="width: 80%" :rules="rules1" ref="formUp">
          <el-form-item label="经纪人名称 :" label-width="20%" prop="name">
            <el-input v-model="formUp.name"></el-input>
          </el-form-item>
          <el-form-item label="星级 :" label-width="20%" prop="level">
            <el-input v-model="formUp.level" placeholder="星级最高为5"></el-input>
          </el-form-item>
          <el-form-item label="联系电话 :" label-width="20%" prop="phone">
            <el-input v-model="formUp.phone"></el-input>
          </el-form-item>
          <el-form-item label="所属公司 :" label-width="20%" prop="company">
            <el-input v-model="formUp.company"></el-input>
          </el-form-item>
          <el-form-item label="所属门店 :" label-width="20%" prop="store">
            <el-input v-model="formUp.store"></el-input>
          </el-form-item>
          <el-form-item label="服务商圈 :" label-width="20%" prop="serviceRange">
            <el-input v-model="formUp.serviceRange"></el-input>
          </el-form-item>
          <el-form-item label="主营楼盘 :" label-width="20%" prop="estate">
            <el-input v-model="formUp.estate"></el-input>
          </el-form-item>
          <el-form-item label="从业时间 :" label-width="20%" prop="seniority">
            <el-input v-model="formUp.seniority"></el-input>
          </el-form-item>
          <el-form-item label="服务宣言 :" label-width="20%" prop="declaration">
            <el-input v-model="formUp.declaration"></el-input>
          </el-form-item>

          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess1"
              :before-upload="beforeAvatarUpload">
              <img v-if="formUp.headImg" :src="formUp.headImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm1('formUp')">取 消</el-button>
          <el-button type="primary" @click="upatAgent1('formUp')">修 改</el-button>
        </div>
      </el-dialog>
      <el-dialog title="查看经纪人" :visible.sync="dialogFormLook">
        <el-form :model="formLook" style="width: 80%" :rules="rules1" ref="formLook">
          <el-form-item label="经纪人名称 :" label-width="20%">
            <el-input v-model="formLook.name"></el-input>
          </el-form-item>
          <el-form-item label="星级 :" label-width="20%">
            <el-input v-model="formLook.level" placeholder="星级最高为5"></el-input>
          </el-form-item>
          <el-form-item label="联系电话 :" label-width="20%">
            <el-input v-model="formLook.phone"></el-input>
          </el-form-item>
          <el-form-item label="所属公司 :" label-width="20%">
            <el-input v-model="formLook.company"></el-input>
          </el-form-item>
          <el-form-item label="所属门店 :" label-width="20%">
            <el-input v-model="formLook.store"></el-input>
          </el-form-item>
          <el-form-item label="服务商圈 :" label-width="20%">
            <el-input v-model="formLook.serviceRange"></el-input>
          </el-form-item>
          <el-form-item label="主营楼盘 :" label-width="20%">
            <el-input v-model="formLook.estate"></el-input>
          </el-form-item>
          <el-form-item label="从业时间 :" label-width="20%">
            <el-input v-model="formLook.seniority"></el-input>
          </el-form-item>
          <el-form-item label="服务宣言 :" label-width="20%">
            <el-input v-model="formLook.declaration"></el-input>
          </el-form-item>
          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="formLook.headImg" :src="formLook.headImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormLook = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormLook = false">确 认</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>

<script>
  import {addAgent, upAgent, getAgent, getAgentid, delAgent,atqueryid} from "@/api/agentApi";

  export default {
    data() {
      let star = (rule, value, callback) => {
        if (/^[1-9]\d*$/.test(value) === false) {
          callback(new Error('必须是正整数'));
        } else if (value > 5) {
          callback(new Error('不能大于5'));
        } else {
          callback();
        }
      };
      let phone1 = (rule, value, callback) => {
        if (/^1[3,4,5,7,8]\d{9}$/.test(value) === false) {
          callback(new Error('手机号有误'));
        } else {
          callback();
        }
      };
      return {
        dialogFormAdd: false,
        dialogFormLook: false,
        dialogFormUp: false,
        currentPage4: 1,
        page: 1,
        size: 5,
        total: 10,
        atid: '',
        sousuo: '',
        formAdd: {
          level: "",
          estate: "",
          seniority: "",
          declaration: "",
          message: "",
          name: "",
          phone: "",
          company: "",
          store: "",
          serviceRange: "",
          headImg: ""
        },
        formUp: {
          level: "",
          estate: "",
          seniority: "",
          declaration: "",
          message: "",
          name: "",
          phone: "",
          company: "",
          store: "",
          serviceRange: "",
          headImg: ""
        },
        formLook: {
          level: "",
          estate: "",
          seniority: "",
          declaration: "",
          message: "",
          name: "",
          phone: "",
          company: "",
          store: "",
          serviceRange: "",
          headImg: ""
        },
        jjrData: [],
        rules1: {
          name: [
            {required: true, message: '请输入经纪人名称', trigger: 'blur'},
          ],
          level: [
            {required: true, message: '请输入星级', trigger: 'blur'},
            {validator: star, trigger: 'blur'},
          ],
          phone: [
            {required: true, message: '请输入手机号码', trigger: 'blur'},
            {validator: phone1, trigger: 'blur'},
          ],
          company: [
            {required: true, message: '请输入公司名称', trigger: 'blur'},
          ],
          store: [
            {required: true, message: '请输入所属门店', trigger: 'blur'},
          ],
          serviceRange: [
            {required: true, message: '请输入服务商圈', trigger: 'blur'},
          ],
          estate: [
            {required: true, message: '请输入主营楼盘', trigger: 'blur'},
          ],
          seniority: [
            {required: true, message: '请输入从业时间', trigger: 'blur'},
          ],
          declaration: [
            {required: true, message: '请输入服务宣言', trigger: 'blur'},
          ],
        },

      }
    },
    methods: {
      getatAgent() {
        let data = {
          page: this.page,
          size: this.size
        };
        this.jjrData = [];
        getAgent(data).then((res) => {
          if (this.sousuo !== '') {
            let data = {number: this.sousuo};
            atqueryid(data).then((res) => {
              if (res.data.data != null) {
                let a = res.data.data;
                this.jjrData.push(a);
              }
            })
          } else {
            this.jjrData = res.data.data.content;
          }

          this.total = res.data.data.totalElements;
        })
      },
      addatAgent(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = this.formAdd;
            addAgent(data).then((res) => {
              this.getatAgent();
              this.resetForm(formName);
              this.$message({
                message: '添加经纪人成功!',
                type: 'success'
              });
            })
          } else {
            this.$message({
              message: '请填写完整!',
              type: 'warning'
            });
            return false;
          }
        });
      },
      lookatAgent(index, row) {
        this.formLook=[];
        let data = {agentid: row.id};
        getAgentid(data).then((res) => {
          this.formLook = res.data.data;
        });
        this.dialogFormLook = true
      },
      upatAgent(index, row) {
        this.atid = row.id;
        let data = {agentid: row.id};
        getAgentid(data).then((res) => {
          this.formUp = res.data.data;
        });
        this.dialogFormUp = true
      },
      upatAgent1(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let id = {agentid: this.atid};
            let data = this.formUp;
            upAgent(id, data).then((res) => {
              this.getatAgent();
              this.resetForm1(formName);
              this.$message({
                message: '修改经纪人成功!',
                type: 'success'
              });
            });
          } else {
            this.$message({
              message: '请填写完整!',
              type: 'warning'
            });
            return false;
          }
        });


      },
      delatAgent(index, row) {
        let data = {agentid: row.id};
        this.$confirm('确认删除吗?', '提示', {
          type: 'warning'
        }).then(() => {
          delAgent(data).then((res) => {
            this.getatAgent();
            this.dialogFormUp = false;
            this.$message({
              message: '删除经纪人成功!',
              type: 'success'
            });
          })

        }).catch(() => {
        });
      },

      sousuoid() {
        this.getatAgent();
      },

      handleSizeChange(val) {
        this.size = val;
        this.getatAgent();
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getatAgent();
      },
      handleAvatarSuccess(res, file) {
        this.formAdd.headImg = res.data;
      },
      handleAvatarSuccess1(res, file) {
        this.formUp.headImg = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      resetForm(formName) {
        this.dialogFormAdd = false;
        this.formAdd.headImg='';
        this.$refs[formName].resetFields();
      },
      resetForm1(formName) {
        this.dialogFormUp = false;
        this.formUp.headImg='';
        this.$refs[formName].resetFields();
      },
    },
    mounted() {
      this.getatAgent();
    },
  }
</script>

<style>

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;

    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
